<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * Class DistributionController
 * @package Index\Controller
 *
 * 说明：分销用户管理后台
 * 作者：caicai
 *
 */

class FenxiaoController extends CommonController
{

    private $table = 'chat_distribution';
    //获取列表
    public function index()
    {
        $this->display();
    }

    /**
     * 添加和修改
     */
    public function add()
    {
        if (IS_POST) { //修改 新增
            $id = I('id');
            $insert = [];
            $insert = [
                'name' => I('name'),
                'code' => I('code'),
            ];

            $viewModel = M($this->table);
            if (!empty($id)) {
                $viewModel->where(['id' => $id])->save($insert);
            } else {

                $viewModel->add($insert);

            }
            $response = array('ret' => '1001', 'code' => '操作成功！');

            $this->ajaxReturn($response, 'json');
        }

        $id = I('id');
        $viewModel = M($this->table);
        if ($id) { //修改
            $data = $viewModel->where(['id' => $id])->find();

            $this->assign(array(
                'data' => $data,
                'id' => $id,

            ));

        }
        $this->assign(array(

            'btnname' => empty($id) ? "添加" : '修改',
        ));
        $this->display();

    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = I('id');
        if (!empty($id)) {
            $model = M($this->table);
            $result = $model->where(array('id' => $id))->delete();
            if ($result) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }
    }

    /**
     * 获取分销url
     */
    public function fxurl()
    {
        $id = session('admin_id');
        $fxcode = session('fxcode');
        $admin_username = session('admin_username');
        if (!$fxcode) {
            $url = '';
        }
        if (!empty($id)) {
            $model = M('chat_userinfo');
            $result = $model->where(array('fxcode' => $fxcode, 'user_phone' => $admin_username))->find();

            if ($result) {
                $url = C('LOCALHOST_URL') . '/Share/new_landing?uid=' . $result['user_id'] . '&fxcode=' . $fxcode;
            } else {
                $url = C('LOCALHOST_URL') . '/Share/new_landing';
            }

            $this->assign('url', $url);
            $this->display();
        }
    }

    public function left(){
        $this->display();
    }
}
